<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/24
 * Time: 8:56
 */

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\Excepiton\ParameterException;

class Tag extends AdminBaseController {
    protected $db;

    protected function _initialize() {
        parent::_initialize();
        $this->db = new \app\admin\model\Tag();
    }

    public function index() {
        $list = db('tag')->paginate(5);
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     *标签的添加和修改控制器
     */
    public function store() {
        $tid = input('param.tid');
        if ($tid){
            //编辑请求
            $oldData = $this->db->find($tid);
        }
        else{
            $oldData = ['tname'=>''];
        }
        if (IS_POST) {
            try{
                $this->db->addOrEditTag($tid);
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('操作失败'.$str,exit);
            }
            $this->success('操作成功','index');
        }
        $this->assign('oldData',$oldData);
        return $this->fetch();
    }


    /**
     *标签的删除
     */
    public function delTag(){
        if (IS_POST){
            try{
                $this->db->del();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('删除标签失败'.$str,exit);
            }
            $this->success('删除标签成功','index');
        }
    }


}